﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using TestItemsSummary.Helpers;
using TestItemsSummary.Models;

namespace TestItemsSummary.Common
{
    public class BusinessProcess
    {
        static SqlHelper sqlHelper = new SqlHelper();
        /// <summary>
        /// 序列化XML文件
        /// </summary>
        /// <param name="path">the path to save the xml file</param>
        /// <param name="obj">the object you want to serialize</param>
        public static void SerializeToXml<T>(string path, T obj) where T : class
        {
            XmlSerializer serializer = new XmlSerializer(obj.GetType());
            string content = string.Empty;
            //serialize
            using (StringWriter writer = new StringWriter())
            {
                serializer.Serialize(writer, obj);
                content = writer.ToString();
            }
            //save to file
            using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.Read))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    sw.Write(content);
                }
            }
        }

        /// <summary>
        /// 反序列XML文件
        /// </summary>
        /// <param name="path">the path of the xml file</param>
        /// <param name="object_type">the object type you want to deserialize</param>
        public static T DeserializeFromXml<T>(string path) where T : class
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            using (StreamReader reader = new StreamReader(path))
            {
                return serializer.Deserialize(reader) as T;
            }
        }
        /// <summary>
        /// 统计测试项详细信息
        /// </summary>
        /// <param name="tEntity">实体类</param>
        /// <param name="dt">输出数据集</param>
        /// <returns></returns>
        public static string StatisticsTestItemsDetails(TestItemsEntity tEntity, out DataTable dt)
        {
            string retMsg = string.Empty;//数据库返回的状态信息
            SqlParameter[] sqlParameters = null;
            try
            {
                sqlParameters = new SqlParameter[] {
                new SqlParameter("@QRPrefix",SqlDbType.NVarChar,50),
                new SqlParameter("@Result",SqlDbType.NVarChar,50),
                new SqlParameter("@Sequence",SqlDbType.NVarChar,50),
                new SqlParameter("@Headers",SqlDbType.NVarChar,50),
                new SqlParameter("@StateCode",SqlDbType.NVarChar,10),
                new SqlParameter("@StateMsg",SqlDbType.NVarChar,4000)
                };
                sqlParameters[0].Value = tEntity.QR ?? "";
                sqlParameters[1].Value = tEntity.Result ?? "";
                sqlParameters[2].Value = tEntity.Sequence ?? "";
                sqlParameters[3].Value = tEntity.Header ?? "";
                sqlParameters[4].Direction = ParameterDirection.Output;
                sqlParameters[5].Direction = ParameterDirection.Output;
                retMsg = sqlHelper.ExecuteStoredProcedureAndReturnDatatable("procSummaryTestItem", sqlParameters, out dt);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return retMsg;
        }
        /// <summary>
        /// 杀掉进程
        /// </summary>
        /// <param name="processName">进程名</param>
        public static void KillProcess(string processName)
        {
            foreach (Process p in Process.GetProcesses())
            {
                if (p.ProcessName.Contains(processName))
                {
                    try
                    {
                        p.Kill();
                        p.WaitForExit(); // possibly with a timeout 
                    }
                    catch (Win32Exception e)
                    {
                        throw new Exception(e.Message);
                    }
                    catch (InvalidOperationException e)
                    {
                        throw new Exception(e.Message);
                    }
                }

            }
        }
    }
}
